<?php
namespace app\mobile\controller;

use app\model\Util; 
use app\model\Theme; 
use app\model\Worker; 
use app\model\Relation; 

class Viewshop extends Base
{
    public function Viewshop()
    {
global $_G, $_P;
$op = !empty($_P['op']) ? $_P['op'] : 'display';
$system=$_G['system'];
$fans = $this->checkuser();
$themeid = intval($_P['themeid']);
$id = intval($_P['id']);
if($themeid < 0 || $id < 0){
	return_msg('活动入口不正确');
}
$recordid = intval($_P['recordid']);
$theme = Theme::getThemeById($themeid);
if(empty($theme['id'])){
    die(Util::showError("请确认活动地址是否正确"));
    return;
}


//员工信息
$myworker = Worker::getThemeWorkerByOpenid($_G['openid'],$themeid,1);
if (empty($myworker)){
	if(!empty($themyrecord)){
		$workerid = $themyrecord['workerid'];
	}else{
		$workerid = intval($_P['workerid']);
	}
	if ($workerid==0){
		$relation = Relation::getByThemeId($themeid,$_G['openid']);
		$workerid = $relation['workerid'];
	}
	if (empty($workerid)){
		if(!empty(Worker::getThemeWorkerByOpenidAlltheme($_G['openid'],1))){
			$theyworker = Worker::CopyThemeWorkerByUid($_G['member']['uid'],$themeid,1);//复制员工信息到新活动
			$workerid = $_G['member']['uid'];
		}
	}elseif (!empty($workerid)){
		$theyworker = Worker::getThemeWorkerByUid($workerid,$themeid,1);
		if (empty($theyworker)){
			$theyworker = Worker::CopyThemeWorkerByUid($workerid,$themeid,1);//复制员工信息到新活动
		}
	}
	$workername = $theyworker['username'];
	$workermobile = $theyworker['mobile'];
	$company = $theyworker['company'];
	$workerqr = $theyworker['qrcode']; 
}else{
	$workerid = $myworker['uid'];
	$workername = $myworker['username'];
	$company = $myworker['company'];
	$workermobile = $myworker['mobile'];
	$workerqr = $myworker['qrcode'];
	
	$sharedata = array(
	    'themeid'=>$themeid,
	    'fopenid'=>$_G['openid'],
	    'fromuid'=>$_G['member']['uid'],
	    'workerid'=>$workerid,
	    'recordid'=>$recordid,
	    'flag'=>1,
	);
	$workerurl =   $_G['siteroot'].createMobileUrl('index',$sharedata);
}

$config = 	Theme::getConfig($themeid);
$cachekey = 'theme'.$_G['mpid'].MODULE_NAME.'-'.$themeid;

    if($system['ruanjian']==3){
		$grouplist = db_select ( "SELECT * FROM " . tablename (t_shangjiagroup) .
				"  order by displayorder" );

    	$shoptiaojian="";
    	if (!empty($company)) {
			$myshop = db_find ( "SELECT * FROM " . tablename (t_shangjia) .
				" WHERE  id = '{$company}' and state = 1 order by displayorder" );
			if ($system['softstyle']!=6) {
				$shoptiaojian=" and id <> '" . $company ."' ";
			}
			$shopname = $myshop['name'];
    	}
	    	
		if (!empty($grouplist)){
			foreach ($grouplist as $index => $item) {
				$shoplist = db_select ( "SELECT * FROM " . tablename (t_shangjia) .
					" WHERE  groupid ='{$item['id']}'  and state = 1 order by displayorder" );
				$grouplist[$index]['shoplist']=$shoplist;
			}
		}else{

			$shoplist = db_select ( "SELECT * FROM " . tablename (t_shangjia) .
					" WHERE  1=1 {$shoptiaojian} and state = 1 order by displayorder" );
			$grouplist[0]['img']= $_G['URL']['DUOSHANGJIA_CSS'] . 'images/free.png';
			$grouplist[0]['name']='免费详情';
			$grouplist[0]['shoplist']=$shoplist;
		}
		$xunichengjiao = intval(db_fetchcolumn ( 'SELECT sum(xunichengjiao) FROM ' . tablename ( t_shangjia ) . "
				WHERE state = 1 " ));
		$totalsum = $totalsum + $xunichengjiao;
    }



$shangjia=db_find("SELECT * FROM ".tablename(t_shangjia)." WHERE id = '{$id}'");
	    if (!empty($shangjia['topimgs'])){
	    	$topimgs=explode(';', $shangjia['topimgs']);
		}



	//分享jssdk
	$canshare = 1;
	if($themyrecord['status'] >= 1 && !empty($themyrecord)){
	    if($recordid <= 0){
	        $recordid = $themyrecord['id'];
	    }
	    if (!empty($config['sharetitle2']) && empty($myworker)){
	    	$sharetitle2 = $config['sharetitle2'];
	    }else{
	    	$sharetitle2 = $theme['title'];
	    }
		$sharetitle = str_replace("NICK",$fans['nickname'],$sharetitle2);
		$sharetitle = str_replace("NAME",$themyrecord['username'],$sharetitle);
		if (!empty($shopname)) {
			$sharetitle = str_replace("SHOPNAME",$shopname,$sharetitle);
		}
		//$theme['title'] = $fans['nickname']."已参加".$theme['title'];
	}else{
	    if (!empty($config['sharetitle1']) && empty($myworker)){
	    	$sharetitle1 = $config['sharetitle1'];
	    }else{
	    	$sharetitle1 = $theme['title'];
	    }
		$sharetitle = str_replace("NICK",$fans['nickname'],$sharetitle1);
		if (!empty($shopname)) {
			$sharetitle = str_replace("SHOPNAME",$shopname,$sharetitle);
		}
	}
	if (!empty($theme['jubf']) ){
		$theme['title'] = $theme['jubf'];
	}
	if (!empty($workername) && $config['isshowkefu']==1){
		$sharetitle = $sharetitle ."【客服:".$workername."】";
	}
	$sharedesc = $theme['desc'];
	if (empty($theme['img'])){
		$shareimgurl = tomedia($config['headlogo']);
	}else{
		$shareimgurl = tomedia($theme['img']);
	}
	$sharedata = array(
	    'themeid'=>$themeid,
	    'fopenid'=>$_G['openid'],
	    'fromuid'=>$_G['member']['uid'],
	    'workerid'=>$workerid,
	    'flag'=>1,
	    'recordid'=>$recordid
	);
	if (empty($myworker)){
		if ($isyixuan){
			$sharedata['fromuid'] = $_P['fromuid']; 
			$sharedata['fopenid']  = $_P['fopenid'];
		}
		$shareurl =   $_G['siteroot'].createMobileUrl('index',$sharedata);
	}else{
		$shareurl = str_replace("recordid","a",$workerurl);
	}
	if ($system['gongzhong']!=1 && rand(1, 12) < 5){
		$shareurl = str_replace($system['s']['domain1'],$system['s']['domain3'],$shareurl);
		$shareurl = str_replace($system['s']['domain2'],$system['s']['domain3'],$shareurl);
	}

//微信获取jssdk
        $register_jssdk = $this->getJsSdkConfig();
        if (empty($_P['show'])) {
	$return_clicent = get_defined_vars();
return view('/duoshangjia/viewshop', $return_clicent);	
}else{
	$return_clicent = get_defined_vars();
return view('/yxpdd/viewshop', $return_clicent);	
}
}
}